<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\CarVin;

class VinController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function getList( Request $request ){

        $res = [];
        $get = $request->all();
        $page = isset( $get['page'] )? intval($get['page']) : 1;
        $number = isset( $get['number'] )? intval($get['number']) : 20;
        $where = [];

        if( !empty( $get['vin'] ) ){
            $where[] =[ 'car_vin.vin', 'like', '%' . $get['vin'] .'%'];
        }

        if( !empty( $get['brand_name'] ) ){
            $where[] =[ 'car_vin.brand_name', 'like', '%' . $get['brand_name'] .'%'];
        }

        if( !empty( $get['factory_name'] ) ){
            $where[] =[ 'car_vin.factory_name', 'like', '%' . $get['factory_name'] .'%'];
        }

        if( !empty( $get['model_name'] ) ){
            $where[] =[ 'car_vin.model_name', 'like', '%' . $get['model_name'] .'%'];
        }

        if( !empty( $get['model_type'] ) ){
            $where[] =[ 'car_vin.model_type', 'like', '%' . $get['model_type'] .'%'];
        }

        if( !empty( $get['engine'] ) ){
            $where[] =[ 'car_vin.engine', 'like', '%' . $get['engine'] .'%'];
        }

        if( !empty( $get['gearbox'] ) ){
            $where[] =[ 'car_vin.gearbox', 'like', '%' . $get['gearbox'] .'%'];
        }

        if( !empty( $get['gearbox_lv'] ) ){
            $where[] =[ 'car_vin.gearbox_lv', 'like', '%' . $get['gearbox_lv'] .'%'];
        }

        if( !empty( $get['year'] ) ){
            $where[] =[ 'car_vin.year', 'like', '%' . $get['year'] .'%'];
        }

        if( !empty( $get['oil_name'] ) ){
            $where[] =[ 'car_oil.name', 'like', '%' . $get['oil_name'] .'%'];
        }

        $list = CarVin::where( $where )
//            ->leftJoin('car_model','car_model.id', '=', 'car_model_object.model_id')
//            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
//            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->leftJoin('car_oil','car_oil.id', '=', 'car_vin.oil_id')
            ->forPage($page, $number)
            ->select(
                'car_vin.id'
                , 'car_vin.vin'
                , 'car_vin.factory_name'
                , 'car_vin.model_name'
                , 'car_vin.model_type'
                , 'car_vin.year'
                , 'car_vin.engine'
                , 'car_vin.gearbox_lv'
                , 'car_vin.gearbox'
                , 'car_oil.name as oil_name'
                )
        ->get();

        $count = CarVin::where( $where )
            ->leftJoin('car_oil','car_oil.id', '=', 'car_vin.oil_id')
            ->count();

        $res = [
            'status'=>0,
            'total'=> $count,
            'page_total'=> ceil( $count / $number),
            'list'=> $list
        ];
        echo json_encode( $res );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function single( $vin ){

        $model = CarVin::where('car_vin.vin',$vin)

            ->leftJoin('car_oil','car_oil.id', '=', 'car_vin.oil_id')
            ->select(
                'car_vin.id',
                'car_vin.brand_name',
                'car_vin.factory_name',
                'car_vin.model_name',
                'car_vin.model_type',

                'car_vin.year',
                'car_vin.engine',
                'car_vin.gearbox_lv',
                'car_vin.gearbox',
                'car_vin.vin',

                'car_vin.info',
                'car_vin.info_lopal',
                'car_vin.source',

                'car_vin.oil_id',
                'car_vin.oil_change',
                'car_vin.oil_regional'

            )
            ->first();
        if( !empty( $model) && !empty($model->info_lopal) ){
            $model->source = 'lopal';
        }
        echo json_encode( $model );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function edit( Request $request, $vin ){

        $post = $request->input();
        unset( $post['api_token']);
        $data = CarVin::edit($vin, $post);
        echo json_encode( $data );
    }

}
